package com.fly.springbootTemplate.controller;

import com.alibaba.fastjson.JSONObject;
import com.fly.springbootTemplate.entity.Admin;
import com.fly.springbootTemplate.service.AdminService;
import com.fly.springbootTemplate.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping(value = "/insertAdmin")
    public Boolean insertAdmin(Admin admin) {
        return adminService.insertAdmin(admin);
    }
    @RequestMapping(value = "/deleteAdmin")
    public Boolean deleteAdmin(Integer id) {
        return adminService.deleteAdmin(id);
    }
    @RequestMapping(value = "/updateAdmin")
    public Boolean updateAdmin(Admin admin) {
        return adminService.updateAdmin(admin);
    }
    @RequestMapping(value = "queryAllAdmin")
    public List<Admin> queryAllAdmin() {
        return adminService.getAllAdmin();
    }
    @RequestMapping(value = "queryAdminById")
    public Admin queryAdminById(Integer id){
        return adminService.getAdminById(id);
    }


    /*
    music
     */
    @RequestMapping(value = "login/status",method = RequestMethod.POST)
    public Object loginStatus(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            boolean flag = adminService.verifyPassword(name, password);
            if (flag) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG,"登录成功!");
                session.setAttribute(Consts.NAME,name);
                return jsonObject;
            }
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG,"用户名或密码错误!");
            return jsonObject;
    }
}
